package com.moody.hibernate.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.GuarantorInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * GuarantorInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.GuarantorInfo
 * @author MyEclipse Persistence Tools
 */

public class GuarantorInfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(GuarantorInfoDAO.class);
	// property constants
	public static final String GUARANTOR_TYPE = "guarantorType";
	public static final String CUST_NO = "custNo";
	public static final String CUST_NAME = "custName";
	public static final String ORGANIZATION_ID = "organizationId";
	public static final String BUSI_LICENSE_ID = "busiLicenseId";
	public static final String IDENTITY_CARD_ID = "identityCardId";
	public static final String PERSONAL_ASSET = "personalAsset";
	public static final String PERSONAL_DEBT = "personalDebt";
	public static final String ANNUAL_INCOME = "annualIncome";
	public static final String CUST_TYPE = "custType";
	public static final String CUST_SCOPE = "custScope";
	public static final String CREDIT_LEVEL = "creditLevel";
	public static final String CREDIT_DATE = "creditDate";
	public static final String IS_OUR_CUSTOMER = "isOurCustomer";

	protected void initDao() {
		// do nothing
	}

	public void save(GuarantorInfo transientInstance) {
		log.debug("saving GuarantorInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GuarantorInfo persistentInstance) {
		log.debug("deleting GuarantorInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GuarantorInfo findById(java.lang.String id) {
		log.debug("getting GuarantorInfo instance with id: " + id);
		try {
			GuarantorInfo instance = (GuarantorInfo) getHibernateTemplate()
					.get("com.moody.hibernate.domain.GuarantorInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GuarantorInfo instance) {
		log.debug("finding GuarantorInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GuarantorInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from GuarantorInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByGuarantorType(Object guarantorType) {
		return findByProperty(GUARANTOR_TYPE, guarantorType);
	}

	public List findByCustNo(Object custNo) {
		return findByProperty(CUST_NO, custNo);
	}

	public List findByCustName(Object custName) {
		return findByProperty(CUST_NAME, custName);
	}

	public List findByOrganizationId(Object organizationId) {
		return findByProperty(ORGANIZATION_ID, organizationId);
	}

	public List findByBusiLicenseId(Object busiLicenseId) {
		return findByProperty(BUSI_LICENSE_ID, busiLicenseId);
	}

	public List findByIdentityCardId(Object identityCardId) {
		return findByProperty(IDENTITY_CARD_ID, identityCardId);
	}

	public List findByPersonalAsset(Object personalAsset) {
		return findByProperty(PERSONAL_ASSET, personalAsset);
	}

	public List findByPersonalDebt(Object personalDebt) {
		return findByProperty(PERSONAL_DEBT, personalDebt);
	}

	public List findByAnnualIncome(Object annualIncome) {
		return findByProperty(ANNUAL_INCOME, annualIncome);
	}

	public List findByCustType(Object custType) {
		return findByProperty(CUST_TYPE, custType);
	}

	public List findByCustScope(Object custScope) {
		return findByProperty(CUST_SCOPE, custScope);
	}

	public List findByCreditLevel(Object creditLevel) {
		return findByProperty(CREDIT_LEVEL, creditLevel);
	}

	public List findByCreditDate(Object creditDate) {
		return findByProperty(CREDIT_DATE, creditDate);
	}

	public List findByIsOurCustomer(Object isOurCustomer) {
		return findByProperty(IS_OUR_CUSTOMER, isOurCustomer);
	}

	public List findAll() {
		log.debug("finding all GuarantorInfo instances");
		try {
			String queryString = "from GuarantorInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public GuarantorInfo merge(GuarantorInfo detachedInstance) {
		log.debug("merging GuarantorInfo instance");
		try {
			GuarantorInfo result = (GuarantorInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GuarantorInfo instance) {
		log.debug("attaching dirty GuarantorInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GuarantorInfo instance) {
		log.debug("attaching clean GuarantorInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GuarantorInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (GuarantorInfoDAO) ctx.getBean("GuarantorInfoDAO");
	}

	public void update(GuarantorInfo guarantorInfo) {
		log.debug("updating GuarantorInfo instance");
		try {
			getHibernateTemplate().update(guarantorInfo);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public List<GuarantorInfo> findCustByCondition(Map<String, String> condition) {
		String hql = "FROM GuarantorInfo WHERE ";
		Set<String> keySet = condition.keySet();
		Iterator<String> it = keySet.iterator();
		while(it.hasNext()){
			String keyString = it.next();
			hql += keyString + " like :" + keyString + " AND ";
		}
		hql = hql.substring(0,hql.length() - 5);
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		
		it = keySet.iterator();
		while(it.hasNext()){
			String keyString = it.next();
			query.setString(keyString, "%" + condition.get(keyString) +"%");
		}
		List<GuarantorInfo> list = query.list();
		session.close();
		return list;
	}
}